package cn.iocoder.yudao.module.biz.controller.admin.customer.serviceUnit;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.biz.controller.admin.customer.serviceUnit.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.customer.serviceUnit.ServiceUnitDO;
import cn.iocoder.yudao.module.biz.service.customer.serviceUnit.ServiceUnitService;

@Tag(name = "管理后台 - 客户档案-服务单元管理")
@RestController
@RequestMapping("/san/biz-customer-archive-service-unit")
@Validated
public class ServiceUnitController {

    @Resource
    private ServiceUnitService serviceUnitService;

    @PostMapping("/create")
    @Operation(summary = "创建客户档案-服务单元管理")
    @PreAuthorize("@ss.hasPermission('san:biz-customer-archive-service-unit:create')")
    public CommonResult<Long> createServiceUnit(@Valid @RequestBody ServiceUnitSaveReqVO createReqVO) {
        return success(serviceUnitService.createServiceUnit(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新客户档案-服务单元管理")
    @PreAuthorize("@ss.hasPermission('san:biz-customer-archive-service-unit:update')")
    public CommonResult<Boolean> updateServiceUnit(@Valid @RequestBody ServiceUnitSaveReqVO updateReqVO) {
        serviceUnitService.updateServiceUnit(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除客户档案-服务单元管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('san:biz-customer-archive-service-unit:delete')")
    public CommonResult<Boolean> deleteServiceUnit(@RequestParam("id") Long id) {
        serviceUnitService.deleteServiceUnit(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除客户档案-服务单元管理")
                @PreAuthorize("@ss.hasPermission('san:biz-customer-archive-service-unit:delete')")
    public CommonResult<Boolean> deleteServiceUnitList(@RequestParam("ids") List<Long> ids) {
        serviceUnitService.deleteServiceUnitListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得客户档案-服务单元管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('san:biz-customer-archive-service-unit:query')")
    public CommonResult<ServiceUnitRespVO> getServiceUnit(@RequestParam("id") Long id) {
        ServiceUnitDO customerArchiveServiceUnit = serviceUnitService.getServiceUnit(id);
        return success(BeanUtils.toBean(customerArchiveServiceUnit, ServiceUnitRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得客户档案-服务单元管理分页")
    @PreAuthorize("@ss.hasPermission('san:biz-customer-archive-service-unit:query')")
    public CommonResult<PageResult<ServiceUnitRespVO>> getServiceUnitPage(@Valid ServiceUnitPageReqVO pageReqVO) {
        PageResult<ServiceUnitDO> pageResult = serviceUnitService.getServiceUnitPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ServiceUnitRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出客户档案-服务单元管理 Excel")
    @PreAuthorize("@ss.hasPermission('san:biz-customer-archive-service-unit:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportServiceUnitExcel(@Valid ServiceUnitPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ServiceUnitDO> list = serviceUnitService.getServiceUnitPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "客户档案-服务单元管理.xls", "数据", ServiceUnitRespVO.class,
                        BeanUtils.toBean(list, ServiceUnitRespVO.class));
    }

    @GetMapping("/getSimpleList")
    @Operation(summary = "获得简易服务单元管理列表")
    @PreAuthorize("@ss.hasPermission('san:biz-customer-archive-service-unit:query')")
    public CommonResult<List<Map<String,Object>>> getSimpleList(@Valid ServiceUnitPageReqVO pageReqVO) {
        return success(serviceUnitService.getSimpleList(pageReqVO));
    }
}